package com.gsta.cdn.cdr.controller;

import com.gsta.cdn.cdr.entity.ResultEntity;
import com.gsta.cdn.cdr.mapper.FileSizeMapper;
import com.gsta.cdn.cdr.mapper.ResolutionTypeMapper;
import com.gsta.cdn.cdr.service.FileSizeService;
import com.gsta.cdn.cdr.service.ResolutionTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(path = "/FileSize")
public class FileSizeController {
    @Autowired
    private FileSizeMapper fileSizeMapper;

    @Resource
    private FileSizeService fileSizeService;

    //全局分热度存储统计
    @RequestMapping(path = "/FileSizeAll", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity FileSizeAll(@RequestParam(required = true) String date,
                                          @RequestParam(required = true) String percent,
                                          @RequestParam(required = false,defaultValue = "1")int page,
                                          @RequestParam(required = false,defaultValue = "200")int pageSize,
                                          HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = fileSizeService.FileSizeAll(date,percent,page,pageSize);
        int total = fileSizeMapper.FileSizeAllCount(date,percent);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(total);
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //热度存储地区列表
    @RequestMapping(path = "/FileSizeArea", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ResolutionTypeArea(@RequestParam(required = true) String date,
                                           @RequestParam(required = true) String percent,
                                           HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = fileSizeService.FileSizeArea(date,percent);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //地区热度存储统计
    @RequestMapping(path = "/FileSizeByArea", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ResolutionTypeByArea(@RequestParam(required = true) String date,
                                             @RequestParam(required = true) String percent,
                                             @RequestParam(required = true) String area,
                                             @RequestParam(required = false,defaultValue = "1")int page,
                                             @RequestParam(required = false,defaultValue = "200")int pageSize,
                                             HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = fileSizeService.FileSizeByArea(date,percent,area,page,pageSize);
        int total = fileSizeMapper.FileSizeByAreaCount(date,percent,area);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(total);
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //PopName热度存储统计
    @RequestMapping(path = "/FileSizeByPopName", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity FileSizeByPopName(@RequestParam(required = true) String date,
                                             @RequestParam(required = true) String percent,
                                             @RequestParam(required = true) String popName,
                                             @RequestParam(required = false,defaultValue = "1")int page,
                                             @RequestParam(required = false,defaultValue = "200")int pageSize,
                                             HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = fileSizeService.FileSizeByPopName(date,percent,popName,page,pageSize);
        int total = fileSizeMapper.FileSizeByPopNameCount(date,percent,popName);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(total);
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //地区的PopName列表
    @RequestMapping(path = "/FileSizeAreaPopName", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity FileSizeAreaPopName(@RequestParam(required = true) String date,
                                                  @RequestParam(required = true) String percent,
                                                  @RequestParam(required = true) String area,
                                                  HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = fileSizeService.FileSizeAreaPopName(date,percent,area);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //地区按PopName存储统计
    @RequestMapping(path = "/FileSizeByAreaPopName", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity FileSizeByAreaPopName(@RequestParam(required = true) String date,
                                                    @RequestParam(required = true) String percent,
                                                    @RequestParam(required = true) String area,
                                                    @RequestParam(required = true) String popName,
                                                    @RequestParam(required = false,defaultValue = "1")int page,
                                                    @RequestParam(required = false,defaultValue = "200")int pageSize,
                                                    HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = fileSizeService.FileSizeByAreaPopName(date,percent,area,popName,page,pageSize);
        int total = fileSizeMapper.FileSizeByAreaPopNameCount(date,percent,area,popName);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(total);
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //热度存储opID列表
    @RequestMapping(path = "/FileSizePopID", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity FileSizePopID(@RequestParam(required = true) String date,
                                           @RequestParam(required = true) String percent,
                                           HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = fileSizeService.FileSizePopID(date,percent);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //PopID热度存储统计
    @RequestMapping(path = "/FileSizeByPopID", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity FileSizeByPopID(@RequestParam(required = true) String date,
                                          @RequestParam(required = true) String percent,
                                          @RequestParam(required = true) String popID,
                                          @RequestParam(required = false,defaultValue = "1")int page,
                                          @RequestParam(required = false,defaultValue = "200")int pageSize,
                                          HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = fileSizeService.FileSizeByPopID(date,percent,popID,page,pageSize);
        int total = fileSizeMapper.FileSizeByPopIDCount(date,percent,popID);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setCount(total);
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }
}
